package com.czwj.timetask;

import java.net.Socket;
import java.util.Calendar;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import com.czwj.entities.Equipment;
import com.czwj.entities.Pressure;
import com.czwj.service.EquipmentService;
import com.czwj.service.PressureService;
import com.czwj.socket.OilPressureSocket;

@Component
@Configuration
@EnableScheduling
public class OilPressureTimerTask {

	@Resource
	EquipmentService equipmentService;
	@Resource
	PressureService pressureService;
	
	@Scheduled(cron = "0 * * * * ?")
	public void CheckWorkStatus() {
		Calendar calendar = Calendar.getInstance();
		List<Pressure> pressures = pressureService.findIsWorking();
		for (Pressure pressure : pressures) {
			Equipment equipment = equipmentService.selectByPrimaryKey(pressure.getEquipmentid());
			if(Math.abs(pressure.getDate().getTime()-calendar.getTimeInMillis())>equipment.getTimeblank()*60*1000) {
				Socket socket = OilPressureSocket.socketMap.get(pressure.getImei());
				if(socket!=null) {
					OilPressureSocket.requirePressureData(socket);
				}
			}
		}
	}
}
